package com.javasm.service.impl;

import com.javasm.dao.impl.GameDaoImpl;
import com.javasm.entity.Game;
import com.javasm.entity.PageInfo;
import com.javasm.service.GameService;

import java.util.List;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/20 11:10
 * @since JDK 1.8
 */
public class GameServiceImpl implements GameService {
    /**
     * 分页查询游戏列表
     *
     * @param page     当前页码
     * @param pageSize 每页查询条数
     * @return
     */
    @Override
    public PageInfo selectGameByPage(int page, int pageSize) {
        List<Game> gameList = new GameDaoImpl().selectGameByPage(page, pageSize);
        int totalCount = new GameDaoImpl().selectTotalCount();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setGameList(gameList);
        pageInfo.setTotalCount(totalCount);
        return pageInfo;
    }

    /**
     * 新增游戏信息
     *
     * @param game 要新增的游戏对象
     * @return
     */
    @Override
    public int insertGameInfo(Game game) {
        String detailpic = game.getDetailpic();
        if (detailpic != null && detailpic.lastIndexOf(";") == detailpic.length() - 1) {  // 删掉多文件上传时文件末尾的分号
            game.setDetailpic(detailpic.substring(0, detailpic.length() - 1));
        }
        return new GameDaoImpl().insertGameInfo(game);
    }

    /**
     * 通过id删除菜单
     *
     * @param gid 要删除的游戏id
     * @return
     */
    @Override
    public int deleteGameInfo(String gid) {
        return new GameDaoImpl().deleteGameInfo(gid);
    }

    /**
     * 通过id查询游戏信息
     *
     * @param gid 游戏id
     * @return
     */
    @Override
    public Game getGameById(String gid) {
        return new GameDaoImpl().getGameById(gid);
    }

    /**
     * 通过id修改菜单信息
     *
     * @param game 要修改的菜单对象
     * @return
     */
    @Override
    public int updateGameInfo(Game game) {
        String detailpic = game.getDetailpic();
        if (detailpic != null && detailpic.lastIndexOf(";") == detailpic.length() - 1) {  // 删掉多文件上传时文件末尾的分号
            game.setDetailpic(detailpic.substring(0, detailpic.length() - 1));
        }
        return new GameDaoImpl().updateGameInfo(game);
    }
}
